package com.zlm.tcp;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Author: 郑
 * tcp文件上传客户端
 */
public class TcpUpload {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9632);
        //创建输出流
        OutputStream os = socket.getOutputStream();
        //创建输入流
        FileInputStream fis = new FileInputStream(new File("ads.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer)) != -1){
            os.write(buffer,0,len);
            //os.flush();
        }
        //socket提供了一个通知服务器数据已传输完毕
        socket.shutdownOutput();
        //接受反馈
        InputStream is = socket.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int l;
        while ((l = is.read(bytes)) !=-1){
            baos.write(bytes,0,l);
            baos.flush();
        }
        System.out.println(baos.toString());
        baos.close();
        fis.close();
        os.close();
        socket.close();

    }
}
